﻿using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Core
{
    public class UILabel : UIWidget, IBindText
    {
        private Text lbl1;

        private TMP_Text lbl2;

        private TblLanguage tbl;

        private string mText = string.Empty;

        public string text
        {
            get => mText;
            set
            {
                mText = value;

                if (lbl2 != null)
                {
                    lbl2.text = value;
                }

                if (lbl1 != null)
                {
                    lbl1.text = value;
                }
            }
        }


        private Color mColor;

        public Color Color
        {
            get => mColor;
            set
            {
                mColor = value;

                if (lbl2 != null)
                {
                    lbl2.color = value;
                }

                if (lbl1 != null)
                {
                    lbl1.color = value;
                }
            }
        }

        public override void Awake(GameObject go)
        {
            base.Awake(go);

            tbl = S.Table.GetSO<TblLanguage>();

            if (go.TryGetComponent(out lbl2))
            {
                mText = lbl2.text;
                mColor = lbl2.color;
            }

            if (go.TryGetComponent(out lbl1))
            {
                mText = lbl1.text;
                mColor = lbl1.color;
            }
        }

        public override void Dispose()
        {
            if (IsDisposed) return;

            lbl1 = null;
            lbl2 = null;

            base.Dispose();
        }

        private void UseWxFont()
        {
            //Text[] allTexts;
            //TextMeshProUGUI[] allTexts;
#if UNITY_WX
            var fallbackFont = "https://www......com/字体文件.ttf";
            WeChatWASM.WX.GetWXFont(fallbackFont, (font) =>
            {
                lbl1.font = font;
                lbl2.font = TMP_FontAsset.CreateFontAsset(font);
            });
#endif
        }
    }
}
